package com.deep.deepenterpise.common.page;

import org.apache.ibatis.session.RowBounds;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author xyz
 * @version 1.0
 * @title DrPageHelper
 * @description TODO 分页工具类
 * @create 2024/4/17 08:36
 */
public class DrPageHelper {

    /**
     * @return RowBounds
     * @Auth xyz
     * @Desc //TODO 获取分页参数
     * @Time 08:54 2024/4/18
     * @Param [pageNum, pageSize]
     */
    public static RowBounds getRowBounds(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return new RowBounds(offset, pageSize);
    }

    /**
     * @return long
     * @Auth xyz
     * @Desc //TODO 计算总页数
     * @Time 08:54 2024/4/18
     * @Param [totalCount, pageSize]
     */
    public static long getTotalPages(Long totalCount, int pageSize) {
        if (totalCount == null || totalCount == 0) {
            return 0;
        }
        BigDecimal totalRecordsBigDecimal = new BigDecimal(totalCount);
        BigDecimal pageSizeBigDecimal = new BigDecimal(pageSize);
        BigDecimal totalPagesBigDecimal = totalRecordsBigDecimal.divide(pageSizeBigDecimal, 0, RoundingMode.CEILING);
        return totalPagesBigDecimal.intValue();
    }
}